package cn.com.nes.site.listener;

import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.iot.RegisterDataItem;
import cn.com.nes.site.entity.iot.RegisterDataItemExcel;
import cn.com.nes.site.module.service.iot.ProtocolService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

public class RegisterDataItemListener implements ReadListener<RegisterDataItemExcel> {

    private final LinkedList<RegisterDataItem> cachedDataList = new LinkedList<>();

    private final ProtocolService protocolService;

    private PageData res;

    private final String protocolId;

    private final String manufactureId;

    private final String username;

    public RegisterDataItemListener(ProtocolService protocolService, String protocolId, String manufactureId, String username) {
        this.protocolService = protocolService;
        this.protocolId = protocolId;
        this.manufactureId = manufactureId;
        this.username = username;
    }

    @Override
    public void invoke(RegisterDataItemExcel data, AnalysisContext analysisContext) {
        cachedDataList.add(convert(data));
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        res = protocolService.importProtocol(manufactureId, username, protocolId, cachedDataList);
        cachedDataList.clear();
    }

    private RegisterDataItem convert(RegisterDataItemExcel data) {
        RegisterDataItem result = new RegisterDataItem();
//        result.setProtocolId(data.getProtocolId());
//        result.setManufactureId(data.getManufactureId());
        result.setRegisterAddress(data.getRegisterAddress());
        result.setBitAddress(data.getBitAddress());
        result.setDataName(data.getDataName());
        result.setDataUnit(data.getDataUnit());
        result.setRegisterType(data.getRegisterType());
        // modify by zhouy
        result.setReadWrite(data.getReadWriteType());
        //result.setWritable(Objects.equals(1, data.getWritable()));
        result.setDataType(data.getDataType());
        result.setFaultDetection(Objects.equals(1, data.getFaultDetection()));
        result.setFaultNormalValue(data.getFaultNormalValue());
        result.setUpperLimit(data.getUpperLimit());
        result.setLowerLimit(data.getLowerLimit());
        result.setFormula(data.getFormula());
        result.setControlFormula(data.getControlFormula());
        result.setDataEnum(data.getDataEnum());
        result.setControlParamEnum(data.getControlParamEnum());
        result.setClassify(data.getClassify());
        result.setCommandCut(data.getCommandCut());
        result.setShowRangeLow(data.getShowRangeLow());
        result.setShowRangeUp(data.getShowRangeUp());
        result.setErrorValue(data.getErrorValue());
        result.setErrorReplaceValue(data.getErrorReplaceValue());

        return result;
    }

    public PageData getRes() {
        return res;
    }
}
